package jinyilw.swing.visuals.text;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.plaf.basic.BasicEditorPaneUI;
import javax.swing.text.EditorKit;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class TransEditorPane extends JEditorPane
{
	private static final long serialVersionUID = 7064090767374375406L;

	public TransEditorPane()
	{

	}

	@Override
	protected EditorKit createDefaultEditorKit()
	{
		return new CenterEditorKit();
	}

	@Override
	public void updateUI()
	{
		setUI(new TransEditorPaneUI());
		invalidate();
	}

	static class TransEditorPaneUI extends BasicEditorPaneUI
	{
		JEditorPane editorPane;
		SimpleAttributeSet attributeSet;

		@Override
		public void installUI(JComponent c)
		{
			super.installUI(c);
			editorPane = (JEditorPane) c;
			editorPane.setOpaque(false);
			editorPane.setSelectionColor(RThemeColor.darkThemeColor);
			editorPane.setSelectedTextColor(RThemeColor.selectedTextColor);
			attributeSet = new SimpleAttributeSet();
			StyleConstants.setFontFamily(attributeSet,
					FontTools.defaultFont.getFamily());
			StyleConstants.setFontSize(attributeSet,
					FontTools.defaultFont.getSize());
		}

		@Override
		public void update(Graphics g, JComponent c)
		{
			if (editorPane.getSelectionColor() != RThemeColor.darkThemeColor)
				editorPane.setSelectionColor(RThemeColor.darkThemeColor);
			Composite composite = ComponentTools
					.applyEnableAlpha((Graphics2D) g, c.isEnabled(), 1f, 0.4f);
			StyleConstants.setForeground(attributeSet,
					RThemeColor.colorSet.textColor);
			StyledDocument doc = (StyledDocument) editorPane.getDocument();
			doc.setCharacterAttributes(0, doc.getLength(), attributeSet, false);
			paint(g, c);
			if (composite != null)
				((Graphics2D) g).setComposite(composite);
		}

		@Override
		protected void paintBackground(Graphics g)
		{

		}
	}
}
